package demo15;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static demo6.Test.test01;

@SuppressWarnings({"all"})
//public class Test {
//    public static void main2(String[] args) throws ClassNotFoundException {
//        Class c1 = Class.forName("demo15.User");
//        System.out.println(c1.getName());
//        System.out.println(c1.getSimpleName());
//        Field[] fields = c1.getFields();
//
//        //获取相关的属性
//        fields = c1.getDeclaredFields();
//        for (Field f : fields) {
//            System.out.println(f);
//        }
//
//        //Method method1=c1.getMethods();
//        //method1=c1.getDeclaredMethod()
//
//    }
//
//    public static void main3(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//        Class c1 = Class.forName("demo15.User");
//
//        User user3 = (User) c1.newInstance();
//        Method setName = c1.getDeclaredMethod("setName", String.class);
//        //invoke就是激活的意思
//        setName.invoke(user3, "张三");
//        System.out.println(user3.getName());
//
//
//        System.out.println("====================================");
//        User user4 = (User) c1.newInstance();
//        Field name = c1.getDeclaredField("name");
//        //下面的这个代码表示的就是关闭我们的这个里面的安全检测，否则就会报错
//        name.setAccessible(true);
//        name.set(user4, "张桑");
//        System.out.println(user4.getName());
//
//
////        User user = (User)c1.newInstance();//---->本质上是调用这个无参数的构造器
////        System.out.println(user);
//
//        //下面的这个成员默认就是私有的，因此我们需要使用这个declared表示无论是私有的还是公有的，都是可以被访问到的
////        Constructor constructor = c1.getDeclaredConstructor(String.class,int.class,int.class);
////        //下面的这个自动创建就是object，因此这个需要强制类型转换
////        User user2 = (User)constructor.newInstance("张三",001,18);
////        System.out.println(user2);
//
//
//    }
//
//
//}
public class Test {
    public static void test01 () {
            User user = new User();
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < 100000000; i++) {
                user.getName();
            }
            long endTime = System.currentTimeMillis();
            System.out.println("正常运行"+(endTime - startTime)+"ms");
        }

        public static void test02 () throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
            User user = new User();
            Class c1=user.getClass();
            Method getName = c1.getDeclaredMethod("getName",null);
            //getName.setAccessible(true);
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < 100000000; i++) {
                getName.invoke(user,null);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("反射的方式,不关闭检测："+(endTime - startTime)+"ms");
        }

        public static void test03 () throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
            User user = new User();
            Class c1=user.getClass();
            Method getName = c1.getDeclaredMethod("getName",null);
            getName.setAccessible(true);
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < 100000000; i++) {
                getName.invoke(user,null);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("反射的方式,关闭检测:"+(endTime - startTime)+"ms");
        }

    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        test01();
        test02();
        test03();
    }
}

class User{
    String name;
    int age;
    int id;
    public User(String name, int age, int id) {};

    public User() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                '}';
    }
}
